package com.xbongbong.paas.service.stage.jump;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasStageEntity;
import com.xbongbong.paas.enums.StageTypeEnum;
import com.xbongbong.paas.service.stage.StageHandle;
import com.xbongbong.paas.service.stage.StageHandleFactory;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.businesstage.pojo.dto.StageJumpValidateDTO;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.CustomerStageErrorCodeEnum;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Auther: yingjianfeng
 * @Date: 2021/12/3 11:03
 * @Description: 阶段跳转
 */
public abstract class AbstractStageJumpHandle {

    @Resource
    private StageHandleFactory stageHandleFactory;

    void startJump() throws XbbException{

    }


    /**
     * 跨阶段跳转校验并设置 from->to 的阶段列表（注：返回值，只有跳转到普通阶段有用到 -> OrdinaryStageJumpHandleImpl）
     * @param stageJumpValidateDTO
     * @return
     * @throws XbbException
     */
    Boolean skipStageValidateAndSetRangeStage(StageJumpValidateDTO stageJumpValidateDTO) throws XbbException {
        PaasStageEntity fromStageEntity = stageJumpValidateDTO.getFromStageEntity();
        PaasStageEntity toStageEntity = stageJumpValidateDTO.getToStageEntity();

        // 跨阶段校验
        Integer fromSort = 99;
        Integer toSort = toStageEntity.getSort();
        Map<String, Object> params = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        if (Objects.nonNull(fromStageEntity)) {
            fromSort = fromStageEntity.getSort();
        }
        boolean isBack = true;
        params.put("corpid", stageJumpValidateDTO.getCorpid());
        params.put("formId", stageJumpValidateDTO.getFormId());
        params.put("stageProcessId", stageJumpValidateDTO.getStageProcessEntity().getId());
        params.put("enable", 1);
        params.put("del", 0);
        if (fromSort > toSort) {
            // 前进
            params.put("sortLess", fromSort);
            params.put("sortGreater", toSort);
            isBack = false;
        } else {
            // 后退
            params.put("sortLess", toSort);
            params.put("sortGreater", fromSort);
            isBack = true;
        }

        //
        StageHandle stageHandle = stageHandleFactory.getStageHandle(stageJumpValidateDTO.getSaasMark(), stageJumpValidateDTO.getBusinessType());
        List<PaasStageEntity> paasStageEntityList = stageHandle.findEntitys(params);
        if (Objects.isNull(paasStageEntityList)) {
            stageJumpValidateDTO.setRangeStageEntitys(new ArrayList<>());
        } else {
            stageJumpValidateDTO.setRangeStageEntitys(paasStageEntityList);
        }

        //1. toStageType 为结束类型时，且允许直接跳到结束阶段 则不进行阶段校验  2. 新建不校验  3. 只校验来自普通阶段或开始阶段的   结束阶段的是校验激活
        if ((StageTypeEnum.getEndType().contains(toStageEntity.getType()) && Objects.equals(toStageEntity.getEnableJumpEnd(), 1)) ||
                (Objects.nonNull(stageJumpValidateDTO.getIsNew()) && stageJumpValidateDTO.getIsNew()) ||
                (!Objects.equals(stageJumpValidateDTO.getFromStageType(), StageTypeEnum.START_STAGE.getType()) && !Objects.equals(stageJumpValidateDTO.getFromStageType(), StageTypeEnum.ORDINARY_STAGE.getType()))
        ) {
            return isBack;
        }

        for (PaasStageEntity temp : paasStageEntityList) {
            Integer allowSkipStage = temp.getAllowSkipStage();
            if (Objects.equals(temp.getType(), StageTypeEnum.ORDINARY_STAGE.getType()) && Objects.equals(allowSkipStage, 0)) {
                //中间有阶段不允许跨过，无法跳转  注：回退也校验
                throw new XbbException(CustomerStageErrorCodeEnum.API_ERROR_238043);
            }
        }
        return isBack;
    }

}
